package com.dmc.android.wordFinder;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import android.app.ListActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.SimpleAdapter;

import com.dmc.scrabblator.WordManager;
import com.dmc.scrabblator.WordRule;
import com.dmc.scrabblator.WordValue;
import com.dmc.scrabblator.WordValueUtil;

public class WordFinderMain extends ListActivity implements OnClickListener,
		OnItemClickListener {
	private String TAG = "WordFinderMain";
	private EditText userTiles;
	private EditText boardRules;
	private SimpleAdapter listAdapter;
	private ArrayAdapter<String> adapter;
	private Button actionButton;

	private static final String STORE_ROW = "word_row1";

	// These constants are used to bind the adapter to the list view
	private static final String[] COLUMN_NAMES = { STORE_ROW };
	private static final int[] VIEW_IDS = { R.id.word_row1 };

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		userTiles = (EditText) findViewById(R.id.EditText01);
		boardRules = (EditText) findViewById(R.id.EditText02);
		actionButton = (Button) findViewById(R.id.button_go);
		actionButton.setOnClickListener(mClickListener);

	}

	private void doWork() {
		List<Map<String, String>> listOfMaps = new ArrayList<Map<String, String>>();
		List<String> result = new ArrayList<String>();
		Map<String, String> singleResult;

		// TODO: Change this to ref the SD card
		File f = new File("/sdcard/DictionaryNewLine.txt");
		WordManager wm = new WordManager();
		try {
			wm.generate(f);
		} catch (Exception e) {
			Log.e(TAG, e.getMessage());
			e.printStackTrace();
			System.exit(1);
		}

		Set<WordValue> strings = wm.search("RTROEIB");// RTRDEIB : birder=10
		WordRule rule = new WordRule();
		List<Character> positions = WordValueUtil.stringToCharList("???0???");// "?2?D4?"
		rule.setRule(positions);
		Set<WordValue> ruledWords = rule.applyRule(strings);

		for (WordValue wv : ruledWords) {
			result.add(wv.getString());
			singleResult = new HashMap<String, String>();
			singleResult.put(STORE_ROW, wv.getString());
			listOfMaps.add(singleResult);
		}

		listAdapter = new SimpleAdapter(this, listOfMaps,
				R.layout.store_result_row, COLUMN_NAMES, VIEW_IDS);
		getListView().setAdapter(listAdapter);
		getListView().setOnItemClickListener(this);
	}

	OnClickListener mClickListener = new OnClickListener()
	{
		public void onClick(View view)
		{
			if (view == actionButton)
			{
				doWork();
			}
			
			
		}

	};
	
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		// TODO Auto-generated method stub

	}

	public void onClick(View v) {
		// TODO Auto-generated method stub

	}
}